package com.design.structural.Bridge.Thermometer.Thermometer;

import com.design.structural.Bridge.Thermometer.Weather_API.globalAPI;
import com.design.structural.Bridge.Thermometer.Weather_API.juheAPI;
import com.design.structural.Bridge.Thermometer.Weather_API.weatherAPI;
import com.design.structural.Bridge.Thermometer.Weather_API.xinzhiAPI;

import java.util.HashMap;

/**
 * 描述: 进度条风格温度计
 *
 * @author YuJunJie
 * @create 2019-04-04 11:04
 */
public class ProcessBarThermometer implements Thermometer{
    private weatherAPI api = null;
    private String apiName;
    static HashMap<String, String> apiMap = new HashMap<>();

    public ProcessBarThermometer(){
        apiMap.put("juhe", "聚合数据api");
        apiMap.put("xinzhi", "心知天气api");
        apiMap.put("global", "全球天气api");
    }

    @Override
    public void setWeatherAPI(weatherAPI api, String apiName) {
        this.api = api;
        this.apiName = apiName;
    }

    @Override
    public void show(String cityName) {
        if (api == null){
            weatherAPI temp = new juheAPI();
            processBarStyle(temp.getWeatherInfo(cityName), "juhe");
            temp = new xinzhiAPI();
            processBarStyle(temp.getWeatherInfo(cityName), "xinzhi");
            temp = new globalAPI();
            processBarStyle(temp.getWeatherInfo(cityName), "global");
        }
        else{
            processBarStyle(api.getWeatherInfo(cityName), apiName);
        }

    }

    private static void processBarStyle(String info, String api){
        System.out.println("\"\"\""+apiMap.get(api)+"\"\"\"");

        String[] infos = info.split(" ");
        System.out.println("所在地: "+infos[0]);
        System.out.print("时间: "+infos[3]);
        System.out.println("当前温度: ");

        int maxTem = 40;
        int now = maxTem * Integer.parseInt(infos[1]) / 50;

        for (int i = 0; i < maxTem; i++) {
            if (i<now){
                System.out.print(">");
            }
            else{
                System.out.print("-");
            }
        }
        System.out.println("  " + Integer.parseInt(infos[1]) + "℃"+" "+infos[2]);
        System.out.println();
    }

}
